<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { FemaleType } from '@/api/check/female/types'

const { required, lengthRange } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<FemaleType>>,
    default: () => null
  },
  formSchema: {
    type: Array as PropType<FormSchema[]>,
    default: () => []
  }
})

const rules = reactive({
  qar_no: [lengthRange({ max: 20 })],
  part_no: [lengthRange({ max: 40 })],
  part_type: [lengthRange({ max: 20 })],
  part_name: [lengthRange({ max: 40 })],
  product_type: [lengthRange({ max: 20 })],
  form_type: [lengthRange({ max: 100 })],
  purchase_order: [lengthRange({ max: 100 })],
  flight: [lengthRange({ max: 30 })],
  quantity_weight: [lengthRange({ max: 20 })],
  vendor: [lengthRange({ max: 40 })],
  rev_letter: [lengthRange({ max: 20 })],
  temper: [lengthRange({ max: 20 })],
  zpm_cpm: [lengthRange({ max: 20 })],
  typing: [lengthRange({ max: 20 })],
  classing: [lengthRange({ max: 20 })],
  grade: [lengthRange({ max: 20 })],
  form: [lengthRange({ max: 20 })],
  size: [lengthRange({ max: 20 })],
  color: [lengthRange({ max: 20 })],
  material_type: [lengthRange({ max: 20 })],
  spec_stan: [lengthRange({ max: 20 })],
  spec_stan_rev: [lengthRange({ max: 20 })],
  mat_code: [lengthRange({ max: 20 })],
  test_items: [lengthRange({ max: 20 })],
  required_dimension: [lengthRange({ max: 20 })],
  pclr_quantity: [lengthRange({ max: 20 })],
  pclr_size: [lengthRange({ max: 20 })],
  plan_remark: [lengthRange({ max: 100 })],
  life_limit_req: [lengthRange({ max: 20 })],
  life_limit_period: [lengthRange({ max: 20 })],
  life_limit_manage: [lengthRange({ max: 20 })],
  special_property: [lengthRange({ max: 20 })],
  manufacturer: [lengthRange({ max: 40 })],
  change_no: [lengthRange({ max: 20 })],
  draw_no: [lengthRange({ max: 20 })],
  draw_no_rev: [lengthRange({ max: 20 })],
  software_code: [lengthRange({ max: 20 })],
  software_rev: [lengthRange({ max: 20 })],
  atp_no: [lengthRange({ max: 20 })],
  atp_rev: [lengthRange({ max: 20 })],
  insp_remark: [lengthRange({ max: 100 })],
  act_measurement: [lengthRange({ max: 20 })],
  product_information: [lengthRange({ max: 40 })],
  product_no: [lengthRange({ max: 40 })],
  product_name: [lengthRange({ max: 40 })],
  product_msn: [lengthRange({ max: 40 })],
  remark: [lengthRange({ max: 100 })],
  serial_no: [lengthRange({ max: 200 })],
  lot_batch_no: [lengthRange({ max: 200 })],
  act_part_rev: [lengthRange({ max: 20 })],
  act_change_no: [lengthRange({ max: 20 })],
  act_draw_no: [lengthRange({ max: 20 })],
  act_draw_no_rev: [lengthRange({ max: 20 })],
  act_software_code: [lengthRange({ max: 20 })],
  act_software_rev: [lengthRange({ max: 20 })],
  act_atp_no: [lengthRange({ max: 20 })],
  act_atp_rev: [lengthRange({ max: 20 })],
  actual_weight: [lengthRange({ max: 20 })],
  source_of_weight: [lengthRange({ max: 20 })],
  acceptable_quant: [lengthRange({ max: 20 })],
  rejective_quant: [lengthRange({ max: 20 })],
  conformity_rate: [lengthRange({ max: 20 })],
  ok_to_file: [lengthRange({ max: 10 })]
})

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    return await getFormData()
  }
}

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="formSchema" :is-col="true" />
</template>

<style lang="less"></style>
